/*
 * @Author: silence
 * @Date: 2022-05-28 15:53:42
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-04-15 16:03:30
 * @Description: 网页
 */
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import '../../../base/SilScaffold.dart';
import '../../../utils/color/ColorUtils.dart';
import 'package:sil_utils_package/sil_utils_package.dart';

class WebViewPage extends StatefulWidget {
  final String? title;
  final String urlPath;

  const WebViewPage(this.urlPath, {Key? key, this.title}) : super(key: key);

  @override
  State<WebViewPage> createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  bool isLoading = true;

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      title: widget.title,
      backgroundColor: ColorUtils.color_bg,
      body: mainWidget(),
      sideMargin: 0,
    );
  }

  mainWidget() {
    if (SilTools.stringIsEmpty(widget.urlPath)) {
      return SilNetErrorWidget(
        prompt: '404 NOT Fund',
        bottomButtonTitle: '',
        reloadFunc: () {
          Navigator.pop(context);
        },
      );
    }

    return Stack(
      children: [
        webView(),
        if (isLoading) pageLoading(),
      ],
    );
  }

  webView() {
    return InAppWebView(
      initialUrlRequest: URLRequest(url: Uri.parse(widget.urlPath)),
      initialOptions: InAppWebViewGroupOptions(
          android: AndroidInAppWebViewOptions(
              mixedContentMode:
                  AndroidMixedContentMode.MIXED_CONTENT_ALWAYS_ALLOW)),
      onProgressChanged: (controller, progress) {},
      onLoadStop: (controller, url) {
        changeLoading();
      },
      onLoadError: (controller, url, code, message) {
        changeLoading();
      },
    );
  }

  void changeLoading() {
    if (isLoading) {
      isLoading = false;
      setState(() {});
    }
  }

  /**
   * 页面内loading
   */
  Widget pageLoading() {
    List<Widget> children = [];

    // 添加一个半透明的遮罩，填充整个屏幕
    children.add(SizedBox.expand(
        child: Container(
      decoration: ShapeDecoration(
          shape: RoundedRectangleBorder(), color: Colors.transparent),
      child: Center(
        child: SizedBox(
          width: 120.w,
          height: 120.w,
          child: Container(
            decoration: ShapeDecoration(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(8.0.w))),
                color: Colors.transparent),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                CircularProgressIndicator(color: ColorUtils.color_theme),
                Padding(padding: EdgeInsets.only(top: 20)),
                SilText(LangOf(context)?.common?.loading,
                    style:
                        TextStyle(fontSize: 12, color: ColorUtils.color_theme)),
              ],
            ),
          ),
        ),
      ),
    )));

    return Stack(children: children);
  }
}
